package us.gaaoc.framework.webapp.backingbeans.admin;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlSelectOneListbox;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import us.gaaoc.framework.businessmodel.Filing;
import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.PersonParticipants;
import us.gaaoc.framework.model.util.CaseRecordSorter;
import us.gaaoc.framework.model.util.DataPage;
import us.gaaoc.framework.model.util.FilingSorter;
import us.gaaoc.framework.model.util.PagedListDataModel;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;

/**
 * 
 * @author alexandrm
 * @date May 8th, 2008
 * 
 */
public class AllCaseRecordsByCaseTypeAndStatus extends BaseBackingBean {

	

	

	/**
	 * 
	 */
	private static final long serialVersionUID = 6467746885095813848L;

	/**
	 * Properties
	 */
	

	private HtmlSelectOneListbox statusUI;

	private int statusCode1;	
	private int statusCode2;
	private int caseType;
	
	private DataModel allCaseRedordsByCaseTypeAndStatusTableList;
	private DataModel allCaseRedordsMissingDefendantsByCaseTypeAndStatusTableList;
	//private List<Filing> childEnforcementFilings;
	private List<CaseRecord> allCaseRecordsByCaseTypeAndStatus;
	private List<CaseRecord> allCaseRecordsMissingDefendantsByCaseTypeAndStatus;
	
	//Variables Used for Sort functionality
	private CaseRecordSorter caseRecordSorter;
	private CaseRecordSorter caseRecordMissingDefendantsSorter;
		
	// Variables Used for Paging ChildEnforcement Filings
	private int childEnforcementRowCount;
	private int allCaseRecordsByCaseTypeAndStatusRowCount;
	private int allCaseRecordsMissingDefendantsByCaseTypeAndStatusRowCount;
	
	private String formReceivedCaseRecordUuid;
	private boolean thereIsDefendant;
	private String defendantName;
	private boolean showDefendantName;
	private boolean showThereIsNoDefendantMessage;
	private List<CaseRecord> caseRecordsToDelete = new ArrayList<CaseRecord>();
	
	private String courtUuid;

	/**
	 * Constructor
	 *
	 */
	@SuppressWarnings("unchecked")
	public AllCaseRecordsByCaseTypeAndStatus() {
		caseRecordSorter = new CaseRecordSorter("instantiationDateTime", this.getAllCaseRecordsByCaseTypeAndStatus());
		//caseRecordMissingDefendantsSorter = new CaseRecordSorter("instantiationDateTime", this.getAllCaseRecordsMissingDefendantsByCaseTypeAndStatus());
		caseRecordMissingDefendantsSorter = new CaseRecordSorter("instantiationDateTime", this.getGAJECaseRecordsMissingDefendants());
		courtUuid = (String)this.getFacesContext().getExternalContext().getSessionMap().get("courtUuid");
	}
	
	/*public AllCaseRecordsByCaseTypeAndStatus(int caseType, int statusCode1){
		this.caseType = caseType;
		this.statusCode1 = statusCode1;
		caseRecordSorter = new CaseRecordSorter("eventDateTime", this.getAllCaseRecordsByCaseTypeAndStatus());
	}

	
	public AllCaseRecordsByCaseTypeAndStatus(int caseType, int statusCode1, int statusCode2){
		this.caseType = caseType;
		this.statusCode1 = statusCode1;
		this.statusCode2 = statusCode2;
		caseRecordSorter = new CaseRecordSorter("eventDateTime", this.getAllCaseRecordsByCaseTypeAndStatus());
	}*/


	

	/**
	 * Getter for property statusUI
	 * @return statusUI
	 */
	public HtmlSelectOneListbox getStatusUI() {
		return statusUI;
	}

	/**
	 * Setter for property statusUI
	 * @param statusUI
	 */
	public void setStatusUI(HtmlSelectOneListbox statusUI) {
		this.statusUI = statusUI;
	}


	/**
	 * @return the childEnforcementRowCount
	 */
	public int getChildEnforcementRowCount() {
		childEnforcementRowCount = this.getModelFacade().findNumberOfCaseFilingsByTypeForAdmin(1, courtUuid);
		return childEnforcementRowCount;
	}

	/**
	 * @param childEnforcementRowCount the childEnforcementRowCount to set
	 */
	public void setChildEnforcementRowCount(int childEnforcementRowCount) {
		this.childEnforcementRowCount = childEnforcementRowCount;
	}

	
	/**8a818166192f7cc201193945f2080291
	 * This method navigates user to the previous page
	 * @return String
	 */
	public String back() {
		return "back";
	}
	
	/**
	 * Method used for paging of large data set with Data Page class
	 */
	private DataPage<CaseRecord> getAllCaseRecordsByCaseTypeAndStatusDataPage(int typeCode, int statusCode1, int statusCode2, int startRow, int pageSize){
        int dataSetSize = getAllCaseRecordsByCaseTypeAndStatusRowCount();
        System.out.println("The number of uploaded and unsentAddition cases is "+dataSetSize);
		if(dataSetSize  >= 10) {
			//System.out.println("The data set size is "+dataSetSize);
			List<CaseRecord> uploadedAndUnsentAdditionCaseRecords = this.getModelFacade().findAllCaseRecordsByCaseTypeAndStatus(typeCode, statusCode1, statusCode2, courtUuid);
			DataPage<CaseRecord> adminSideAllCaseRedordsByCaseTypeAndStatusTableListForpage = new DataPage<CaseRecord>(dataSetSize, startRow, uploadedAndUnsentAdditionCaseRecords);
			caseRecordSorter.sort("eventDateTime", caseRecordSorter.isAscending(),uploadedAndUnsentAdditionCaseRecords);
			return adminSideAllCaseRedordsByCaseTypeAndStatusTableListForpage;
		}
		else {
			//System.out.println("Inside the last data set. The data set size is "+dataSetSize);
			List<CaseRecord> uploadedAndUnsentAdditionCaseRecords = this.getModelFacade().findAllCaseRecordsByCaseTypeAndStatus(typeCode, statusCode1, statusCode2, courtUuid);
			DataPage<CaseRecord> adminSideAllCaseRedordsByCaseTypeAndStatusTableListForpage = new DataPage<CaseRecord>(dataSetSize, startRow, uploadedAndUnsentAdditionCaseRecords);
			caseRecordSorter.sort("eventDateTime", caseRecordSorter.isAscending(),uploadedAndUnsentAdditionCaseRecords);
			return adminSideAllCaseRedordsByCaseTypeAndStatusTableListForpage;
		}
	}
	
	/**
	 * Method used for paging of large data set with Data Page class
	 */
	private DataPage<CaseRecord> getAllCaseRecordsMissingDefendantsByCaseTypeAndStatusDataPage(int typeCode, int statusCode1, int statusCode2, int startRow, int pageSize){
        int dataSetSize = getAllCaseRecordsMissingDefendantsByCaseTypeAndStatusRowCount();
        System.out.println("The number of uploaded and unsentAddition cases missing defendants is "+dataSetSize);
		if(dataSetSize  >= 10) {
			//System.out.println("The data set size is "+dataSetSize);
			List<CaseRecord> uploadedAndUnsentAdditionCaseRecordsMissingDefendants = this.getGAJECaseRecordsMissingDefendants();
			//List<CaseRecord> uploadedAndUnsentAdditionCaseRecordsMissingDefendants = this.getModelFacade().findAllCaseRecordsByCaseTypeAndStatus(typeCode, statusCode1, statusCode2);
			DataPage<CaseRecord> adminSideAllCaseRedordsMissingDefendantsByCaseTypeAndStatusTableListForpage = new DataPage<CaseRecord>(dataSetSize, startRow, uploadedAndUnsentAdditionCaseRecordsMissingDefendants);
			caseRecordSorter.sort("eventDateTime", caseRecordSorter.isAscending(),uploadedAndUnsentAdditionCaseRecordsMissingDefendants);
			return adminSideAllCaseRedordsMissingDefendantsByCaseTypeAndStatusTableListForpage;
		}
		else {
			//System.out.println("Inside the last data set. The data set size is "+dataSetSize);
			List<CaseRecord> uploadedAndUnsentAdditionCaseRecordsMissingDefendants = this.getGAJECaseRecordsMissingDefendants();
			//List<CaseRecord> uploadedAndUnsentAdditionCaseRecordsMissingDefendants = this.getModelFacade().findAllCaseRecordsByCaseTypeAndStatus(typeCode, statusCode1, statusCode2);
			DataPage<CaseRecord> adminSideAllCaseRedordsMissingDefendantsByCaseTypeAndStatusTableListForpage = new DataPage<CaseRecord>(dataSetSize, startRow, uploadedAndUnsentAdditionCaseRecordsMissingDefendants);
			caseRecordSorter.sort("eventDateTime", caseRecordSorter.isAscending(),uploadedAndUnsentAdditionCaseRecordsMissingDefendants);
			return adminSideAllCaseRedordsMissingDefendantsByCaseTypeAndStatusTableListForpage;
		}
	}
	

	/**
	 * Internal Class used for paging of New Filings Data
	 * 
	 * @author mcelratr
	 *
	 */
	private class AllCaseRecordsByCaseTypeAndStatusDataModel extends PagedListDataModel {
		public AllCaseRecordsByCaseTypeAndStatusDataModel(int pageSize) {
            super(pageSize);
        }
        
        public DataPage<CaseRecord> fetchPage(int startRow, int pageSize) {
            // call enclosing managed bean method to fetch the data
        	return AllCaseRecordsByCaseTypeAndStatus.this.getAllCaseRecordsByCaseTypeAndStatusDataPage(1, 7, 15, startRow, pageSize);
        }
    }
	
	/**
	 * Internal Class used for paging of New Filings Data
	 * 
	 * @author mcelratr
	 *
	 */
	private class AllCaseRedordsMissingDefendantsByCaseTypeAndStatusDataModel extends PagedListDataModel {
		public AllCaseRedordsMissingDefendantsByCaseTypeAndStatusDataModel(int pageSize) {
            super(pageSize);
        }
        
        public DataPage<CaseRecord> fetchPage(int startRow, int pageSize) {
            // call enclosing managed bean method to fetch the data
        	return AllCaseRecordsByCaseTypeAndStatus.this.getAllCaseRecordsMissingDefendantsByCaseTypeAndStatusDataPage(1, 7, 15, startRow, pageSize);
        }
    }
	
	/**
	 *  This method returns the defendant in a GAJE case record
	 *  One use of this method is when the application breaks when trying to 
	 *  display tables of different filings. One possible reason is that a case record was created without a defendant
	 *  but a filing is a view drawing from, among other tables, the participants table therefore the application breaks.
	 *  The admin then can delete all these records
	 */
	public String getDefendants(String caseRecordUuid) {
		System.out
				.println("Inside the getDefendants method!! The passed uuid is "
						+ this.formReceivedCaseRecordUuid);
		//String uuid = this.formReceivedCaseRecordUuid;
		String uuid = caseRecordUuid;
		CaseRecord caseRecord = this.getModelFacade().findCaseRecordById(uuid, courtUuid);
		List<PersonParticipants> caseRecordDefendantParticipants = new ArrayList<PersonParticipants>();
		caseRecordDefendantParticipants = this.getModelFacade()
				.getPersonParticipantByParticipantRelationshipCode(caseRecord, 2);		
		if ((caseRecordDefendantParticipants != null) && (caseRecordDefendantParticipants.size() != 0)) {
			System.out.println("There is a defendant");
			PersonParticipants defendantParticipant = caseRecordDefendantParticipants.get(0);
			this.thereIsDefendant = true;
			String defendantPersonUuid = defendantParticipant.getPerson().getUuid();
			System.out.println("The defendant uuid is " + defendantPersonUuid);
			Person defendantPerson = this.getModelFacade().findPersonbyID(defendantPersonUuid);
			if (defendantPerson != null) {
				this.defendantName = defendantPerson.getFirstName() + " "
						+ defendantPerson.getLastName();
				this.showDefendantName = true;
			}
		}else{
			System.out.println("There is NO defendant");
			this.setShowThereIsNoDefendantMessage(true);
			this.setThereIsDefendant(true);
		}
		return "stay";
	} 
	/**
	 * This method displays the case records with status uploaded or unsent addition
	 * that are missing a defendant. The page will show the list of these records 
	 * and a prompt for deleting them
	 * @return List of GAJE case records missing defendants that must be deleted
	 */
	public List<CaseRecord> getGAJECaseRecordsMissingDefendants(){
		System.out.println("Deleting.. ");
		List<CaseRecord> caseRecords = this.getModelFacade().findAllCaseRecordsByCaseTypeAndStatus(1, 7, 15, courtUuid);
		List<CaseRecord> caseRecordsToDelete = new ArrayList<CaseRecord>();
		for(CaseRecord cr :caseRecords ){
			String uuid = cr.getUuid();
			CaseRecord caseRecord = this.getModelFacade().findCaseRecordById(uuid, courtUuid);
			List<PersonParticipants> caseRecordDefendantParticipants = new ArrayList<PersonParticipants>();
			caseRecordDefendantParticipants = this.getModelFacade().getPersonParticipantByParticipantRelationshipCode(caseRecord, 2);
			if(caseRecordDefendantParticipants.size() == 0){
				caseRecordsToDelete.add(cr);
			}
			
		}
		//this.getModelFacade().deleteCaseRecord(this.formReceivedCaseRecordUuid);
		this.caseRecordsToDelete = caseRecordsToDelete;
		return caseRecordsToDelete;
	}
	
	public String deleteGAJECaseRecordsMissingDefendants(){
		for(CaseRecord cr: this.caseRecordsToDelete){
			this.getModelFacade().deleteCaseRecord(cr.getUuid(), courtUuid);
		}
		return "stay";		
	}

	public String goToAllCaseRecordsByCaseTypeAndStatus(){
		System.out.println("Inside the goToAllCaseRecordsByCaseTypeAndStatus method");
		return "goToAllCaseRecordsByCaseTypeAndStatusPage";
	}



	public DataModel getAllCaseRedordsByCaseTypeAndStatusTableList() {
		allCaseRedordsByCaseTypeAndStatusTableList = new AllCaseRecordsByCaseTypeAndStatusDataModel(10);
		return allCaseRedordsByCaseTypeAndStatusTableList;
	}



	public void setAllCaseRedordsByCaseTypeAndStatusTableList(
			DataModel allCaseRedordsByCaseTypeAndStatusTableList) {
		this.allCaseRedordsByCaseTypeAndStatusTableList = allCaseRedordsByCaseTypeAndStatusTableList;
	}
	

	public DataModel getAllCaseRedordsMissingDefendantsByCaseTypeAndStatusTableList() {
		allCaseRedordsMissingDefendantsByCaseTypeAndStatusTableList = new AllCaseRedordsMissingDefendantsByCaseTypeAndStatusDataModel(10);
		return allCaseRedordsMissingDefendantsByCaseTypeAndStatusTableList;
	}

	public void setAllCaseRedordsMissingDefendantsByCaseTypeAndStatusMissigDefendantsTableList(
			DataModel allCaseRedordsMissingDefendantsByCaseTypeAndStatusTableList) {
		this.allCaseRedordsMissingDefendantsByCaseTypeAndStatusTableList = allCaseRedordsMissingDefendantsByCaseTypeAndStatusTableList;
	}



	public int getStatusCode1() {
		return statusCode1;
	}



	public void setStatusCode1(int statusCode1) {
		this.statusCode1 = statusCode1;
	}



	public int getStatusCode2() {
		return statusCode2;
	}



	public void setStatusCode2(int statusCode2) {
		this.statusCode2 = statusCode2;
	}



	public int getCaseType() {
		return caseType;
	}



	public void setCaseType(int caseType) {
		this.caseType = caseType;
	}

	

	public CaseRecordSorter getCaseRecordSorter() {
		return caseRecordSorter;
	}

	public void setCaseRecordSorter(CaseRecordSorter caseRecordSorter) {
		this.caseRecordSorter = caseRecordSorter;
	}

	public String getFormReceivedCaseRecordUuid() {
		return formReceivedCaseRecordUuid;
	}

	public void setFormReceivedCaseRecordUuid(String formReceivedCaseRecordUuid) {
		this.formReceivedCaseRecordUuid = formReceivedCaseRecordUuid;
	}

	public boolean isThereIsDefendant() {
		return thereIsDefendant;
	}

	public void setThereIsDefendant(boolean thereIsDefendant) {
		this.thereIsDefendant = thereIsDefendant;
	}

	public String getDefendantName() {
		return defendantName;
	}

	public void setDefendantName(String defendantName) {
		this.defendantName = defendantName;
	}

	public boolean isShowDefendantName() {
		return showDefendantName;
	}

	public void setShowDefendantName(boolean showDefendantName) {
		this.showDefendantName = showDefendantName;
	}

	public boolean isShowThereIsNoDefendantMessage() {
		return showThereIsNoDefendantMessage;
	}

	public void setShowThereIsNoDefendantMessage(
			boolean showThereIsNoDefendantMessage) {
		this.showThereIsNoDefendantMessage = showThereIsNoDefendantMessage;
	}

	public CaseRecordSorter getCaseRecordMissingDefendantsSorter() {
		return caseRecordMissingDefendantsSorter;
	}

	public void setCaseRecordMissingDefendantsSorter(
			CaseRecordSorter caseRecordMissingDefendantsSorter) {
		this.caseRecordMissingDefendantsSorter = caseRecordMissingDefendantsSorter;
	}
	
	public List<CaseRecord> getAllCaseRecordsByCaseTypeAndStatus() {
		return allCaseRecordsByCaseTypeAndStatus;
	}

	public void setAllCaseRecordsByCaseTypeAndStatus(
			List<CaseRecord> allCaseRecordsByCaseTypeAndStatus) {
		this.allCaseRecordsByCaseTypeAndStatus = allCaseRecordsByCaseTypeAndStatus;
	}

	public List<CaseRecord> getAllCaseRecordsMissingDefendantsByCaseTypeAndStatus() {
		return allCaseRecordsMissingDefendantsByCaseTypeAndStatus;
	}

	public void setAllCaseRecordsMissingDefendantsByCaseTypeAndStatus(
			List<CaseRecord> allCaseRecordsMissingDefendantsByCaseTypeAndStatus) {
		this.allCaseRecordsMissingDefendantsByCaseTypeAndStatus = allCaseRecordsMissingDefendantsByCaseTypeAndStatus;
	}


	public int getAllCaseRecordsByCaseTypeAndStatusRowCount() {
		allCaseRecordsByCaseTypeAndStatusRowCount = this.getModelFacade().findNumberOfAllCaseRecordsByCaseTypeAndStatus(1, 7, 15, courtUuid);		
		return allCaseRecordsByCaseTypeAndStatusRowCount;
	}

	public void setAllCaseRecordsByCaseTypeAndStatusRowCount(int allCaseRecordsByCaseTypeAndStatusRowCount) {
		this.allCaseRecordsByCaseTypeAndStatusRowCount = allCaseRecordsByCaseTypeAndStatusRowCount;
	}

	public int getAllCaseRecordsMissingDefendantsByCaseTypeAndStatusRowCount() {
		List<CaseRecord> caseRecordMissingDefendants = this.getGAJECaseRecordsMissingDefendants();//this.getModelFacade().findAllCaseRecordsByCaseTypeAndStatus(1, 7, 15);		
		//allCaseRecordsMissingDefendantsByCaseTypeAndStatusRowCount = this.getModelFacade().findNumberOfAllCaseRecordsByCaseTypeAndStatus(1, 7, 15);
		allCaseRecordsMissingDefendantsByCaseTypeAndStatusRowCount = caseRecordMissingDefendants.size();
		return allCaseRecordsMissingDefendantsByCaseTypeAndStatusRowCount;
	}

	public void setAllCaseRecordsMissingDefendantsByCaseTypeAndStatusRowCount(
			int allCaseRecordsMissingDefendantsByCaseTypeAndStatusRowCount) {
		this.allCaseRecordsMissingDefendantsByCaseTypeAndStatusRowCount = allCaseRecordsMissingDefendantsByCaseTypeAndStatusRowCount;
	}

	public List<CaseRecord> getCaseRecordsToDelete() {
		return caseRecordsToDelete;
	}

	public void setCaseRecordsToDelete(List<CaseRecord> caseRecordsToDelete) {
		this.caseRecordsToDelete = caseRecordsToDelete;
	}



}